package com.xiaotu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaotu.model.*;
import com.xiaotu.service.RentApplyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者 王艳龙
 * @日期 2021-03-10 17:10
 * @功能
 */
@RestController
@CrossOrigin
@RequestMapping("/rentManager")
public class RentApplyController {

    @Autowired
    private RentApplyService rentApplyService;

    /**
     * 获取企业房租通申请记录
     * @param rentModel
     * @return
     */
   @PostMapping("/queryRentApplyInfo")
   public Object queryRentApplyInfo(TabEnterpriseRentApplyInfo rentModel,Integer pageNum,Integer pageSize){
       Map<String,Object> map = new HashMap<String,Object>();
       if(null == pageNum){
           pageNum =1;
       }
       if(null == pageSize){
           pageSize =10;
       }
       PageHelper.startPage(pageNum, pageSize);
       Page<Object> page = PageHelper.getLocalPage();
       List<TabEnterpriseRentApplyInfo> rentApplyInfoList =  rentApplyService.queryRentApplyInfo(rentModel);
       map.put("rentApplyInfoList",rentApplyInfoList);
       map.put("total", page.getTotal());
       map.put("pageCount", page.getPages());
       if(null != rentModel.getId()){
           TabEnterpriseRentInfo rentInfoModel = rentApplyService.queryRentInfo(rentModel.getId());
           map.put("rentInfoModel",rentInfoModel);
       }else{
           map.put("rentInfoModel","");
       }
       return map;
   }

    /**
     * 后台获取房租通申请记录
     * @param pageNum
     * @param pageSize
     * @param authId
     * @param approverId
     * @param enterpriseName
     * @param declareYear
     * @param applyStatusDesc
     * @return
     */
    @PostMapping("/queryRentApplyByAuthId")
    public Object queryRentApplyByAuthId(Integer pageNum,Integer pageSize,String authId,String approverId,
          String enterpriseName,String declareYear,String applyStatusDesc){
        if(null == pageNum){
            pageNum =1;
        }
        if(null == pageSize){
            pageSize =10;
        }
        Map<String,Object> map = new HashMap<String,Object>();
        if (StringUtils.isEmpty(authId)) {
            throw new java.lang.IllegalArgumentException("请提供菜单权限id");
        }
        if (StringUtils.isEmpty(approverId)) {
            throw new java.lang.IllegalArgumentException("请提供当前企业登录id");
        }
        PageHelper.startPage(pageNum, pageSize);
        Page<Object> page = PageHelper.getLocalPage();
        List<Map<String,Object>> rentApplyInfoList =  rentApplyService.queryRentApplyByAuthId(
                authId,approverId,enterpriseName,declareYear,applyStatusDesc);
        map.put("data",rentApplyInfoList);
        map.put("total", page.getTotal());
        map.put("pageCount", page.getPages());
        return map;
    }

    /**
     * 新增修改房租通申请
     * @param rentApplyModel
     * @param rentInfoModel
     * @return
     */
    @PostMapping("/addOrupdateRentInfo")
    public Object addOrupdateRentInfo(TabEnterpriseRentApplyInfo rentApplyModel,TabEnterpriseRentInfo rentInfoModel){
        return  rentApplyService.addOrupdateRentInfo(rentApplyModel,rentInfoModel);
    }

    /**
     * 根据id删除房租通申请
     * @param id
     * @return
     */
    @PostMapping("/delRentApplyInfo")
    public Object delRentApplyInfo(Integer id){
        rentApplyService.delRentApplyInfo(id);
        return null;
    }


    /**
     * 获取所属园区名称
     * @param enterpriseParkId
     * @return
     */
    @PostMapping("/queryEnterpriseParkName")
    public Object queryEnterpriseParkName(String enterpriseParkId){
        return rentApplyService.queryEnterpriseParkName(enterpriseParkId);
    }

    /**
     * 根据parentId获取大中小分类名称
     * @param model
     * @return
     */
    @PostMapping("/queryCodeNameByParentId")
    public Object queryCodeNameByParentId(TabCultureRelatedInfo model){
        Map<String,Object> map = new HashMap<String,Object>();
        List<TabCultureRelatedInfo> data =  rentApplyService.queryCodeNameByParentId(model);
        map.put("data",data);
        return  map;
    }

    /**
     * 获取文化行业代码记录信息
     * @param model
     * @return
     */
    @PostMapping("/queryCultureRelatedInfo")
    public Object queryCultureRelatedInfo(TabCultureRelatedInfo model){
        Map<String,Object> map = new HashMap<String,Object>();
        List<String> list = new ArrayList<String>();
        list.add(model.getCodeName());
        StringBuffer codeName = rentApplyService.getAllCodeName(model,list);
        if(codeName.length()>0){
            model.setCodeName(codeName.toString());
        }
        map.put("data",model);
        return  map;
    }

    /**
     * 根据编码校验是否存在信息
     * @param model
     * @return
     */
    @PostMapping("/queryVerifyClassCode")
    public Object queryVerifyClassCode(TabCultureRelatedInfo model){
        Map<String,Object> map = new HashMap<String,Object>();
        TabCultureRelatedInfo classModle = rentApplyService.queryVerifyClassCode(model.getGuildClassCode());
        TabCultureAllRelatedInfo allCodelModel = rentApplyService.queryVerifyAllClassCode(model.getGuildClassCode());
        if(null !=classModle){
            List<String> list = new ArrayList<String>();
            list.add(classModle.getCodeName());
            model.setParentId(classModle.getParentId());
            StringBuffer codeName = rentApplyService.getAllCodeName(model,list);
            if(codeName.length()>0){
                classModle.setCodeName(codeName.toString());
                //model.setCodeName(codeName.toString());
            }
            map.put("data",classModle);
            map.put("status",1);
            return map;
        }if(null !=allCodelModel){
            List<String> list = new ArrayList<String>();
            list.add(allCodelModel.getCodeName());
            model.setParentId(allCodelModel.getParentId());
            StringBuffer codeName = rentApplyService.getAllCodeName(model,list);
            if(codeName.length()>0){
                allCodelModel.setCodeName(codeName.toString());
               // model.setCodeName(codeName.toString());
            }
            map.put("data",allCodelModel);
            map.put("status",2);
            return  map;
        }else{
            map.put("data","");
            map.put("status",0);
            return  map;
        }

    }

    /**
     * 获取企业经营状况记录
     * @param applyId
     * @return
     */
    @PostMapping("/queryStateOperationInfo")
    public Object queryShareholderInfo(String applyId){
        Map<String,Object> map = new HashMap<String,Object>();
        //房租通股东信息记录
        List<TabEnterpriseRentShareholderInfo> shareholderOneList = rentApplyService.queryShareholderInfo(applyId,1);
        map.put("shareholderOneList",shareholderOneList);
        //房租通关联企业记录
        List<TabEnterpriseRentShareholderInfo> shareholderTwoList = rentApplyService.queryShareholderInfo(applyId,2);
        map.put("shareholderTwoList",shareholderTwoList);
        //房租通所获荣誉记录
        List<TabEnterpriseRentHonorInfo> honorList = rentApplyService.queryHonorInfo(applyId);
        map.put("honorList",honorList);
        //房租通账户信息记录
        List<TabEnterpriseRentAccountInfo> accountList = rentApplyService.queryAccountInfo(applyId);
        map.put("accountList",accountList);
        return  map;
    }

    /**
     * 新增修改企业经营状况记录
     * @param shareholderInfo
     * @return
     */
    @PostMapping("/addOrUpdateShareholderInfo")
    public Object addOrUpdateShareholderInfo(TabEnterpriseRentShareholderInfo shareholderInfo){
        Map<String,Object> map = new HashMap<String,Object>();
        rentApplyService.addOrUpdateShareholderInfo(shareholderInfo);
        return  map;
    }
    /**
     * 新增修改企业所获荣誉信息
     * @param honorInfo
     * @return
     */
    @PostMapping("/addOrUpdateHonorInfo")
    public Object addOrUpdateHonorInfo(TabEnterpriseRentHonorInfo honorInfo){
        Map<String,Object> map = new HashMap<String,Object>();
        rentApplyService.addOrUpdateHonorInfo(honorInfo);
        return  map;
    }

    /**
     * 新增修改企业账户信息
     * @param accountInfo
     * @return
     */
    @PostMapping("/addOrUpdateAccountInfo")
    public Object addOrUpdateAccountInfo(TabEnterpriseRentAccountInfo accountInfo){
        Map<String,Object> map = new HashMap<String,Object>();
        rentApplyService.addOrUpdateAccountInfo(accountInfo);
        return  map;
    }

    /**
     * 删除企业房租通股东信息和关联企业记录
     * @param id
     * @return
     */
    @PostMapping("/delShareholderInfo")
    public Object delShareholderInfo(Integer id){
        rentApplyService.delShareholderInfo(id);
        return null;
    }

    /**
     * 删除企业所获荣誉记录
     * @param id
     * @return
     */
    @PostMapping("/delHonorInfo")
    public Object delHonorInfo(Integer id){
        rentApplyService.delHonorInfo(id);
        return null;
    }

    /**
     * 删除企业账户信息记录
     * @param id
     * @return
     */
    @PostMapping("/delAccountInfo")
    public Object delAccountInfo(Integer id){
        rentApplyService.delAccountInfo(id);
        return null;
    }

    /**
     * 获取企业联系人记录
     * @param applyId
     * @return
     */
    @PostMapping("/queryContactsInfo")
    public Object queryContactsInfo(Integer applyId){
        Map<String,Object> map = new HashMap<String,Object>();
        TabEnterpriseRentContactsInfo data = rentApplyService.queryContactsInfo(applyId);
        map.put("data",data);
        return  map;
    }

    /**
     * 新增修改企业联系人记录
     * @param model
     * @return
     */
    @PostMapping("/addOrUpdateContactsInfo")
    public Object addOrUpdateContactsInfo(TabEnterpriseRentContactsInfo model){
        if(null!=model.getId()){
            rentApplyService.updateContactsInfo(model);
        }else{
            rentApplyService.addContactsInfo(model);
        }
        return null;
    }

    /**
     * 审批流程完成后，同步企业基本信息
     * @param enterpriseId
     * @param applyId
     * @return
     */
    @PostMapping("/autosynEnterpriseInfo")
        public Object autosynEnterpriseInfo(String enterpriseId,String applyId){
        rentApplyService.autosynEnterpriseInfo(enterpriseId,applyId);
        return  null;
    }

}
